package com.android.androidintellectguess;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AndroidintellectguessActivity extends Activity {
	private static final String TAG = Utils.TAG;
	private static final String digits = "3"; //default

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Utils.applyFonts(((ViewGroup)findViewById(android.R.id.content)).getChildAt(0),Typeface.createFromAsset(getAssets(), Utils.FONTDEFAULT));
		
		final AlertDialog.Builder builder4Single = new AlertDialog.Builder(this);
		builder4Single.setTitle("Which game mode do you prefer?");
		builder4Single.setIcon(R.drawable.icon);
		Button singlebtn = (Button) findViewById(R.id.singlebtn);
		singlebtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				builder4Single
				.setPositiveButton(Utils.NORMAL, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						singleOnClick(v);
					}
				})
				.setNeutralButton(Utils.ATTACK, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						attackOnClick(v);
					}
				})
				.setNegativeButton(Utils.SURVIVAL, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						survivalOnClick(v);
					}
				});
				builder4Single.create().show();
			}
			
		});
		
		Button quitbtn = (Button) findViewById(R.id.quitbtn);
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(Utils.STAYWITHME)
			.setPositiveButton(Utils.BUTTON_OK,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				})
			.setNegativeButton(Utils.BUTTON_CANCEL,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						finish();

					}
				})
			.setTitle("Why?");
		
		quitbtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				builder.create().show();
			}
		});
	}

	public void singleOnClick(View view) {
		Intent i = new Intent(this, Single.class);
		i.putExtra("digits", digits);
		startActivityForResult(i, Utils.REQUEST_CODE);
	}
	
	public void attackOnClick(View view) {
		Intent i = new Intent(this, AttackMode.class);
		i.putExtra("digits", digits);
		i.putExtra("gamemode", Utils.ATTACK);
		i.putExtra("timeLeft", 0L);
		startActivityForResult(i, Utils.REQUEST_CODE);
	}
	
	public void survivalOnClick(View view) {
		Intent i = new Intent(this, Survival.class);
		i.putExtra("digits", digits);
		i.putExtra("gamemode", Utils.SURVIVAL);
		startActivityForResult(i, Utils.REQUEST_CODE);
	}
	
	public void multipleOnClick(View view){
		Intent i = new Intent(this, Multiple.class);
		startActivityForResult(i, Utils.REQUEST_CODE);
	}
	
	public void howtoplayOnClick(View view){
		Intent i = new Intent(this, Help.class);
		startActivityForResult(i, Utils.REQUEST_CODE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.i(TAG, "onActivityResult");
		if (resultCode == RESULT_OK && requestCode == Utils.REQUEST_CODE) {
			if (data.hasExtra("digits")) {
				String strDigits = data.getExtras().getString("digits");
				String gamemode = data.getExtras().getString("gamemode");
				if (!strDigits.equals("0")){
					Intent i = new Intent();
					if (gamemode.equals(Utils.NORMAL))
						i = new Intent(this, Single.class);
					else if (gamemode.equals(Utils.ATTACK))
						i = new Intent(this, AttackMode.class);
					else if (gamemode.equals(Utils.SURVIVAL))
						i = new Intent(this, Survival.class);
					i.putExtra("digits", strDigits);
					if (data.hasExtra("limitTries")) i.putExtra("limitTries", data.getExtras().getInt("limitTries"));
					i.putExtra("gamemode", gamemode);
					i.putExtra("timeLeft", data.getExtras().getLong("timeLeft"));
					i.putExtra("finishingTimes", data.getExtras().getInt("finishingTimes"));
					startActivityForResult(i, Utils.REQUEST_CODE);
				}
			}
		}
	}
}
